package net.bolue.web.mvc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bolue.console.data.InsertInfo;
import net.bolue.console.data.UpdateInfo;
import net.bolue.console.data.model.InsertInfoModel;
import net.bolue.console.data.model.UpdateInfoModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.InsResModel;
import net.bolue.sysmng.model.MdbopResModel;
import net.bolue.sysmng.model.UpdResModel;
import net.bolue.sysmng.tblbean.InsRes;
import net.bolue.sysmng.tblbean.MdbopRes;
import net.bolue.sysmng.tblbean.MdbopSubRes;
import net.bolue.sysmng.tblbean.UpdRes;
import net.bolue.web.entry.FormTable;
import net.bolue.web.report.RepConstants;
import net.bolue.web.util.FormUtil;
import net.bolue.web.util.RequestUtil;

/**
 * 初始化组合数据维护操作
 * @author Administrator
 *
 */
public class InitMDBOpAction extends AbstractAction{

	@Override
	public ActionResult perform(HttpServletRequest request,
			HttpServletResponse response) throws BolueException {
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		String mdbopID = RequestUtil.getParamValue(request, RepConstants.REP_MDBOP_KEY);
		if(subSys==null || subSys.trim().length()==0 || mdbopID==null || mdbopID.trim().length()==0){
			throw new BolueException(1, "新增URL中必须提供subSys及insertID参数值!");
		}
		MdbopResModel mdbopResModel = new MdbopResModel(request);
		MdbopRes mdbopRes = mdbopResModel.getMdbopRes(subSys, mdbopID);
		ActionResult actionResult = new ActionResult();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		pageMap.put("subSys", mdbopRes.getSubSys());
		pageMap.put("mdbopId", mdbopRes.getMdbopId());
		pageMap.put("mdbopRes", mdbopRes);
		List<MdbopSubRes> mdbopSubResList = mdbopResModel.getMdbopSubRes(subSys, mdbopID);
		List<FormTable> ftList = new ArrayList<FormTable>();
		for(MdbopSubRes mdbopSubRes : mdbopSubResList){
			String dbopResId = mdbopSubRes.getDbopResId();
			Integer dbopType = mdbopSubRes.getDbopType();
			String dbopSubSys = mdbopSubRes.getDbopSubSys();
			FormTable formTable = null;
			if(dbopType==1){//新增资源
				InsResModel insResModel = new InsResModel();
				InsRes insRes = insResModel.getInsRes(dbopSubSys, dbopResId);
				InsertInfoModel insertInfoModel = new InsertInfoModel(request,insRes);
				InsertInfo insertInfo = insertInfoModel.getInsertInfo();
				formTable = FormUtil.getFTForInsert(insertInfo);
			}else if(dbopType==2){//修改资源
				UpdResModel updResModel = new UpdResModel();
				UpdRes updRes = updResModel.getUpdRes(dbopSubSys, dbopResId);
				UpdateInfoModel updateInfoModel = new UpdateInfoModel(request,updRes);
				UpdateInfo updInfo = updateInfoModel.getUpdInfo();
				formTable = FormUtil.getFTForUpd(updInfo);
			}
			if(formTable==null){
				continue;
			}
			formTable.setFtSign(mdbopSubRes.getSubResId());
			ftList.add(formTable);
		}
		pageMap.put("formTables", ftList);
		actionResult.setData(pageMap);
		actionResult.setResult(true);
		return actionResult;
	}

}
